package com.share.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.Cabinet;
import com.share.device.domain.Station;
import com.share.device.domain.vo.StationVo;
import com.share.device.service.CabinetService;
import com.share.device.service.StationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 站点管理接口
 * 提供站点的增删改查、详情、下拉框等操作
 */
@Tag(name = "站点接口管理")
@RestController
@RequestMapping("/station")
public class StationController extends BaseController
{
    @Autowired
    private StationService stationService;
    @Autowired
    private CabinetService cabinetService;

    /**
     * 查询站点列表
     * @param station 查询参数
     * @return 站点列表数据
     */
    @Operation(summary = "查询站点列表")
    @GetMapping("/list")
    public TableDataInfo list(Station station)
    {
        startPage(); // 启动分页
        List<Station> list = stationService.selectStationList(station); // 查询站点列表
        return getDataTable(list); // 返回分页数据
    }

    /**
     * 新增站点
     * @param station 站点信息
     * @return 操作结果
     */
    @Operation(summary = "新增站点")
    @PostMapping
    public AjaxResult add(@RequestBody Station station)
    {
        return toAjax(stationService.saveStation(station));
    }

    /**
     * 修改站点
     * @param station 站点信息
     * @return 操作结果
     */
    @Operation(summary = "修改站点")
    @PutMapping
    public AjaxResult edit(@RequestBody Station station)
    {
        return toAjax(stationService.updateStation(station));
    }

    /**
     * 获取站点详细信息
     * @param id 站点ID
     * @return 站点详细信息
     */
    @Operation(summary = "获取站点详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        //根据id获取站点详细信息
        Station station = stationService.getById(id);
        //根据站点id获取该站点的所有柜机
        List<Cabinet> list = cabinetService.list(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getStationId, id));
        StationVo stationVo = new StationVo();
        stationVo.setName(station.getName());
        stationVo.setFullAddress(station.getAddress());
        stationVo.setCabinetList(list);
        return success(stationVo);
    }

    /**
     * 删除站点
     * @param ids 站点ID数组
     * @return 操作结果
     */
    @Operation(summary = "删除站点")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stationService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 站点设置关联数据
     * @param station 站点信息
     * @return 操作结果
     */
    @Operation(summary = "站点设置关联数据")
    @PostMapping("/setData")
    public AjaxResult setData(@RequestBody Station station)
    {
        return toAjax(stationService.setData(station));
    }

    /**
     * 获取站点下拉框
     * @return
     */
    @Operation(summary = "获取站点下拉框")
    @GetMapping("/select")
    public AjaxResult select()
    {
        List<Station> list = stationService.list();
        return success(list);
    }
    @Operation(summary = "更新初始化数据")
    @GetMapping("/updateData")
    public AjaxResult updateData()
    {
        stationService.updateData();
        return success();
    }
}
